using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Data.SqlClient;
using System.ServiceModel;

namespace SsbTransportTestSuite
{
    class TestUtils
    {
        //static string connstring;
        internal static int TestTimeout = 180000; //test timeout in milliseconds
        internal static string ServiceEndpointAddress = "net.ssb:source=Client1:target=Service1";
        internal static string ClientEndpointAddress = "net.ssb:source=*:target=Client1";

        internal static string GetConnectionString(string application)
        {
            string connstring = ConfigurationManager.ConnectionStrings["ssbTest"].ConnectionString;
            if (application != null && application.Length > 0)
            {
                SqlConnectionStringBuilder cb = new SqlConnectionStringBuilder(connstring);
                cb.ApplicationName = application;
                return cb.ToString();
            }
            return connstring;

            
        }

        internal static void ServiceHost_Faulted(object sender, EventArgs e)
        {
            Console.WriteLine("ServiceHost Faulted {0} {1}", sender, e);
        }

        internal static string GetConnectionString()
        {

            return GetConnectionString("");
        }

        internal static void ValidatePostTestQueueState()
        {
            SqlConnection cn = new SqlConnection(TestUtils.GetConnectionString());
            cn.Open();
            SqlCommand cmd = new SqlCommand(
                                            @"  
                                            declare @conversations int      
                                            set @conversations = 
                                            (
                                            select count(*) from
                                            sys.conversation_endpoints
                                            where state = 'CO'
                                            )
                                            if @conversations > 0
                                            begin
                                              raiserror('There are %d active conversations after the test',16,1,@conversations)
                                            end", cn);
            cmd.ExecuteNonQuery();
            cn.Close();

        }
        internal static void ResetQueues()
        {
            SqlConnection cn = new SqlConnection(TestUtils.GetConnectionString());
            cn.Open();
            SqlCommand cmd = new SqlCommand(
                                            @"declare @handle uniqueidentifier
                                            declare conv cursor for select conversation_handle from
                                            sys.conversation_endpoints 
                                            open conv
                                            fetch NEXT FROM conv into @handle
                                            while @@FETCH_STATUS = 0
                                            Begin
                                            END Conversation @handle with cleanup
                                            fetch NEXT FROM conv into @handle
                                            End
                                            close conv
                                            deallocate conv
                                            alter queue Service1Queue WITH STATUS=ON
                                            alter queue Client1Queue WITH STATUS=ON", cn);
            cmd.CommandTimeout = 0;
            cmd.ExecuteNonQuery();
            cn.Close();

        }
    }
}
